<!DOCTYPE html>

<html lang="
zh-CN"
    >

        <head>
            
                <title>
                    
                        JAVA_javaweb -
                            
                                shanX&#39;s Blog
                </title>
                <meta charset="UTF-8">
                <meta name="description" content="">
                <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=5">
                
                    

                        <link rel="shortcut icon" href="/image/author.png" type="image/png" />
                        <meta name="description" content="基本概念">
<meta property="og:type" content="article">
<meta property="og:title" content="JAVA_javaweb">
<meta property="og:url" content="https://rhymexmove.github.io/2021/04/30/6cdc310e6784/index.html">
<meta property="og:site_name" content="shanX&#39;s Blog">
<meta property="og:description" content="基本概念">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://rhymexmove.github.io/2021/04/30/6cdc310e6784/image/article/JAVA-javaweb-20210430/20210428131402.png">
<meta property="og:image" content="https://rhymexmove.github.io/2021/04/30/6cdc310e6784/image/article/JAVA-javaweb-20210430/20210428131746.png">
<meta property="og:image" content="https://rhymexmove.github.io/2021/04/30/6cdc310e6784/image/article/JAVA-javaweb-20210430/20210428132115.png">
<meta property="og:image" content="https://rhymexmove.github.io/2021/04/30/6cdc310e6784/image/article/JAVA-javaweb-20210430/20210428133849.png">
<meta property="og:image" content="https://rhymexmove.github.io/2021/04/30/6cdc310e6784/image/article/JAVA-javaweb-20210430/20210428133859.png">
<meta property="og:image" content="https://rhymexmove.github.io/2021/04/30/6cdc310e6784/image/article/JAVA-javaweb-20210430/20210428135406.png">
<meta property="og:image" content="https://rhymexmove.github.io/2021/04/30/6cdc310e6784/image/article/JAVA-javaweb-20210430/20210428162308.png">
<meta property="og:image" content="https://rhymexmove.github.io/2021/04/30/6cdc310e6784/image/article/JAVA-javaweb-20210430/20210428162631.png">
<meta property="og:image" content="https://rhymexmove.github.io/2021/04/30/6cdc310e6784/image/article/JAVA-javaweb-20210430/20210428162806.png">
<meta property="og:image" content="https://rhymexmove.github.io/2021/04/30/6cdc310e6784/image/article/JAVA-javaweb-20210430/20210428170220.png">
<meta property="og:image" content="https://rhymexmove.github.io/2021/04/30/6cdc310e6784/image/article/JAVA-javaweb-20210430/20210428170647.png">
<meta property="og:image" content="https://rhymexmove.github.io/2021/04/30/6cdc310e6784/image/article/JAVA-javaweb-20210430/20210428171043.png">
<meta property="og:image" content="https://rhymexmove.github.io/2021/04/30/6cdc310e6784/image/article/JAVA-javaweb-20210430/20210428171331.png">
<meta property="og:image" content="https://rhymexmove.github.io/2021/04/30/6cdc310e6784/image/article/JAVA-javaweb-20210430/20210428172011.png">
<meta property="og:image" content="https://rhymexmove.github.io/2021/04/30/6cdc310e6784/image/article/JAVA-javaweb-20210430/20210428201314.png">
<meta property="og:image" content="https://rhymexmove.github.io/2021/04/30/6cdc310e6784/image/article/JAVA-javaweb-20210430/20210428230921.png">
<meta property="og:image" content="https://rhymexmove.github.io/2021/04/30/6cdc310e6784/image/article/JAVA-javaweb-20210430/20210430181656.png">
<meta property="og:image" content="https://rhymexmove.github.io/2021/04/30/6cdc310e6784/image/article/JAVA-javaweb-20210430/20210430200702.png">
<meta property="article:published_time" content="2021-04-30T15:48:28.000Z">
<meta property="article:modified_time" content="2022-04-07T17:25:27.690Z">
<meta property="article:author" content="shanX">
<meta property="article:tag" content="java基础">
<meta property="article:tag" content="狂神说">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://rhymexmove.github.io/2021/04/30/6cdc310e6784/image/article/JAVA-javaweb-20210430/20210428131402.png">
                            <link rel="stylesheet" href="https://cdn.jsdelivr.net/combine/npm/highlight.js@9.15.8/styles/atom-one-dark.css,gh/theme-nexmoe/hexo-theme-nexmoe@latest/source/lib/mdui_043tiny/css/mdui.css,gh/theme-nexmoe/hexo-theme-nexmoe@latest/source/lib/iconfont/iconfont.css,gh/fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.css?v=233"
                                crossorigin>
                            <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/justifiedGallery@3.8.1/dist/css/justifiedGallery.min.css">
                            <!--适用于 Retina 屏的 iPad-->
                            <link rel="apple-touch-icon-precomposed" sizes="144x144" href="https://cdn.jsdelivr.net/gh/RhymeXmove/blogimg@latest/author.png">
                            <!--适用于 Retina 屏的 iPhone-->
                            <link rel="apple-touch-icon-precomposed" sizes="120x120" href="https://cdn.jsdelivr.net/gh/RhymeXmove/blogimg@latest/author.png">
                            <!--适用于非 Retina 屏的 iPad-->
                            <link rel="apple-touch-icon-precomposed" sizes="72x72" href="https://cdn.jsdelivr.net/gh/RhymeXmove/blogimg@latest/author.png">
                            <!--适用于非 Retina 屏的 iPhone-->
                            <link rel="apple-touch-icon-precomposed" href="https://cdn.jsdelivr.net/gh/RhymeXmove/blogimg@latest/author.png">
                            
                                <link rel="stylesheet" href="//at.alicdn.com/t/font_2490857_fmjyoao96km.css">
                                
                                    <link rel="stylesheet" href="/css/style.css?v=1649858550917">
        <meta name="generator" content="Hexo 5.4.0"></head>

        <body class="mdui-drawer-body-left">
            
                <div id="nexmoe-background">
                    <div class="nexmoe-bg" style="background-image: url(https://cdn.jsdelivr.net/gh/RhymeXmove/blogimg@latest/cover/5887c2f1-b191-4890-aab5-2ce62d9955df.jpg)"></div>
                    <div class="mdui-appbar mdui-shadow-0">
                        <div class="mdui-toolbar">
                            <a mdui-drawer="{target: '#drawer', swipe: true}" title="menu" class="mdui-btn mdui-btn-icon mdui-ripple"><i class="mdui-icon nexmoefont icon-menu"></i></a>
                            <div class="mdui-toolbar-spacer"></div>
                            <!--<a href="javascript:;" class="mdui-btn mdui-btn-icon"><i class="mdui-icon material-icons">search</i></a>-->
                            <a href="/" title="shanX" class="mdui-btn mdui-btn-icon"><img src="https://cdn.jsdelivr.net/gh/RhymeXmove/blogimg@latest/author.png" alt="shanX"></a>
                        </div>
                    </div>
                </div>
                <div id="nexmoe-header">
                    <div class="nexmoe-drawer mdui-drawer" id="drawer">
    <div class="nexmoe-avatar mdui-ripple">
        <a href="/" title="shanX">
            <img src="https://cdn.jsdelivr.net/gh/RhymeXmove/blogimg@latest/author.png" alt="shanX" alt="shanX">
        </a>
    </div>
    <div class="nexmoe-count nexmoe-widget-wrap">
        <div><span>文章</span>30</div>
        <div><span>标签</span>16</div>
        <div><span>分类</span>8</div>
    </div>
    <div class="nexmoe-list mdui-list nexmoe-widget-wrap" mdui-collapse="{accordion: true}">
        
        <a class="nexmoe-list-item mdui-list-item mdui-ripple" href="/" title="回到首页">
            <i class="mdui-list-item-icon nexmoefont icon-home"></i>
            <div class="mdui-list-item-content">
                回到首页
            </div>
        </a>
        
        <a class="nexmoe-list-item mdui-list-item mdui-ripple" href="/archives.html" title="文章归档">
            <i class="mdui-list-item-icon nexmoefont icon-container"></i>
            <div class="mdui-list-item-content">
                文章归档
            </div>
        </a>
        
        <a class="nexmoe-list-item mdui-list-item mdui-ripple" href="/cesium-test/index.html" title="Cesium">
            <i class="mdui-list-item-icon nexmoefont icon-Cesium-copy"></i>
            <div class="mdui-list-item-content">
                Cesium
            </div>
        </a>
        
        <a class="nexmoe-list-item mdui-list-item mdui-ripple" href="/cesium-covid/index.html" title="全球疫情">
            <i class="mdui-list-item-icon nexmoefont icon-areachart"></i>
            <div class="mdui-list-item-content">
                全球疫情
            </div>
        </a>
        
        <a class="nexmoe-list-item mdui-list-item mdui-ripple" href="/about.html" title="关于博客">
            <i class="mdui-list-item-icon nexmoefont icon-info-circle"></i>
            <div class="mdui-list-item-content">
                关于博客
            </div>
        </a>
        
    </div>
    <aside id="nexmoe-sidebar">
  
  <div class="nexmoe-widget-wrap">
    <div class="nexmoe-widget nexmoe-search">
        <form id="search_form" action_e="https://cn.bing.com/search?q=rhymexmove.github.io" onsubmit="return search();">
            <label><input id="search_value" name="q" type="search" placeholder="搜索"></label>
        </form>
    </div>
</div>
  
  <div class="nexmoe-widget-wrap">
    <div class="nexmoe-widget nexmoe-social">
        <a class="mdui-ripple" href="https://space.bilibili.com/20666214" target="_blank" mdui-tooltip="{content: '哔哩哔哩'}" style="color: rgb(231, 106, 141);background-color: rgba(231, 106, 141, .15);">
            <i class="nexmoefont icon-bilibili"></i>
        </a><a class="mdui-ripple" href="https://github.com/RhymeXmove/" target="_blank" mdui-tooltip="{content: 'GitHub'}" style="color: rgb(231, 106, 141);background-color: rgba(231, 106, 141, .15);">
            <i class="nexmoefont icon-github"></i>
        </a><a class="mdui-ripple" href="https://gitee.com/shanxin123/" target="_blank" mdui-tooltip="{content: 'Gitee'}" style="color: rgb(220,20,60);background-color: rgba(220,20,60, .15);">
            <i class="nexmoefont icon-gitee"></i>
        </a>
    </div>
</div>
  
  
    <div class="nexmoe-widget-wrap">
        <h3 class="nexmoe-widget-title">
            文章分类
        </h3>
        <div class="nexmoe-widget">

            <ul class="category-list">

                


                    

                        

                            <li class="category-list-item">
                                <a class="category-list-link" href="/categories/mysql/">
                                    -mysql
                                </a>
                                <span class="category-list-count">1</span>
                            </li>

                            

                            <li class="category-list-item">
                                <a class="category-list-link" href="/categories/Cesium/">
                                    Cesium
                                </a>
                                <span class="category-list-count">3</span>
                            </li>

                            

                            <li class="category-list-item">
                                <a class="category-list-link" href="/categories/DB/">
                                    DB
                                </a>
                                <span class="category-list-count">2</span>
                            </li>

                            

                            <li class="category-list-item">
                                <a class="category-list-link" href="/categories/Leetcode/">
                                    Leetcode
                                </a>
                                <span class="category-list-count">4</span>
                            </li>

                            

                            <li class="category-list-item">
                                <a class="category-list-link" href="/categories/Vue/">
                                    Vue
                                </a>
                                <span class="category-list-count">2</span>
                            </li>

                            

                            <li class="category-list-item">
                                <a class="category-list-link" href="/categories/blog搭建/">
                                    blog搭建
                                </a>
                                <span class="category-list-count">1</span>
                            </li>

                            

                            <li class="category-list-item">
                                <a class="category-list-link" href="/categories/java/">
                                    java
                                </a>
                                <span class="category-list-count">12</span>
                            </li>

                            

                            <li class="category-list-item">
                                <a class="category-list-link" href="/categories/资源/">
                                    资源
                                </a>
                                <span class="category-list-count">1</span>
                            </li>

                            
            </ul>

        </div>
    </div>
    
  
  
  <div class="nexmoe-widget-wrap">
    <div id="randomtagcloud" class="nexmoe-widget tagcloud nexmoe-rainbow">
      <a href="/tags/DB/" style="font-size: 10px;">-DB</a> <a href="/tags/Cesium/" style="font-size: 12.5px;">Cesium</a> <a href="/tags/GIS/" style="font-size: 10px;">GIS</a> <a href="/tags/Leetcode/" style="font-size: 15px;">Leetcode</a> <a href="/tags/Vue/" style="font-size: 12.5px;">Vue</a> <a href="/tags/blog%E6%90%AD%E5%BB%BA/" style="font-size: 10px;">blog搭建</a> <a href="/tags/echart/" style="font-size: 10px;">echart</a> <a href="/tags/java/" style="font-size: 10px;">java</a> <a href="/tags/java%E5%9F%BA%E7%A1%80/" style="font-size: 20px;">java基础</a> <a href="/tags/mysql/" style="font-size: 10px;">mysql</a> <a href="/tags/spring/" style="font-size: 10px;">spring</a> <a href="/tags/vue/" style="font-size: 10px;">vue</a> <a href="/tags/%E6%89%B9%E5%A4%84%E7%90%86%E5%B7%A5%E5%85%B7/" style="font-size: 10px;">批处理工具</a> <a href="/tags/%E6%95%B0%E6%8D%AE%E5%BA%93/" style="font-size: 10px;">数据库</a> <a href="/tags/%E7%8B%82%E7%A5%9E%E8%AF%B4/" style="font-size: 17.5px;">狂神说</a> <a href="/tags/%E8%B5%84%E6%BA%90/" style="font-size: 10px;">资源</a>
    </div>
    
  </div>

  
  
  <div class="nexmoe-widget-wrap">
    <h3 class="nexmoe-widget-title">文章归档</h3>
    <div class="nexmoe-widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2022/">2022</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/">2021</a><span class="archive-list-count">29</span></li></ul>
    </div>
  </div>



  
  
  <div class="nexmoe-widget-wrap">
    <h3 class="nexmoe-widget-title">最新文章</h3>
    <div class="nexmoe-widget">
      <ul>
        
          <li>
            <a href="/2022/04/13/7533e8f3cb4c/">mysql查询数据库内全部表名；查询表内全部字段名</a>
          </li>
        
          <li>
            <a href="/2021/09/26/ee931b51117c/">什么是计算机图形学？</a>
          </li>
        
          <li>
            <a href="/2021/09/08/9adeea845004/">中国城市坐标(包含市辖区)</a>
          </li>
        
          <li>
            <a href="/2021/08/31/7ba201dda6cb/">Cesium billboard贴地形表</a>
          </li>
        
          <li>
            <a href="/2021/08/09/0d3ed7d31d83/">Vue中使用Vue-jsonp请求jsonp数据</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
    <div class="nexmoe-copyright">
        &copy; 2022 shanX
        Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
        & <a href="https://github.com/theme-nexmoe/hexo-theme-nexmoe" target="_blank">Nexmoe</a>
        
    </div>
</div><!-- .nexmoe-drawer -->
                </div>
                <div id="nexmoe-content">
                    <div class="nexmoe-primary">
                        <div class="nexmoe-post">

  <div class="nexmoe-post-right">
    
      <div class="nexmoe-fixed">
        <div class="nexmoe-valign">
            <div class="nexmoe-toc">
                
                
                  <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5"><span class="toc-number">1.</span> <span class="toc-text">基本概念</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%89%8D%E8%A8%80"><span class="toc-number">1.1.</span> <span class="toc-text">前言</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#web%E5%BA%94%E7%94%A8%E7%A8%8B%E5%BA%8F"><span class="toc-number">1.2.</span> <span class="toc-text">web应用程序</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%9D%99%E6%80%81web"><span class="toc-number">1.3.</span> <span class="toc-text">静态web</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8A%A8%E6%80%81web"><span class="toc-number">1.4.</span> <span class="toc-text">动态web</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Web%E6%9C%8D%E5%8A%A1%E5%99%A8"><span class="toc-number">2.</span> <span class="toc-text">Web服务器</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#web%E6%9C%8D%E5%8A%A1%E5%99%A8"><span class="toc-number">2.1.</span> <span class="toc-text">web服务器</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#IIS"><span class="toc-number">2.1.1.</span> <span class="toc-text">IIS</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Tomcat"><span class="toc-number">2.1.2.</span> <span class="toc-text">Tomcat</span></a></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Tomcat-1"><span class="toc-number">3.</span> <span class="toc-text">Tomcat</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Tomcat%E5%90%AF%E5%8A%A8%E5%92%8C%E9%85%8D%E7%BD%AE"><span class="toc-number">3.1.</span> <span class="toc-text">Tomcat启动和配置</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8F%91%E5%B8%83%E4%B8%80%E4%B8%AA%E7%BD%91%E7%AB%99"><span class="toc-number">3.2.</span> <span class="toc-text">发布一个网站</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#HTTP"><span class="toc-number">4.</span> <span class="toc-text">HTTP</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BB%80%E4%B9%88%E6%98%AFHTTP"><span class="toc-number">4.1.</span> <span class="toc-text">什么是HTTP</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%A4%E4%B8%AA%E6%97%B6%E4%BB%A3"><span class="toc-number">4.2.</span> <span class="toc-text">两个时代</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#HTTP%E8%AF%B7%E6%B1%82"><span class="toc-number">4.3.</span> <span class="toc-text">HTTP请求</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%AF%B7%E6%B1%82%E8%A1%8C"><span class="toc-number">4.3.1.</span> <span class="toc-text">请求行</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B6%88%E6%81%AF%E5%A4%B4"><span class="toc-number">4.3.2.</span> <span class="toc-text">消息头</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#HTTP%E5%93%8D%E5%BA%94"><span class="toc-number">4.4.</span> <span class="toc-text">HTTP响应</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%93%8D%E5%BA%94%E4%BD%93"><span class="toc-number">4.4.1.</span> <span class="toc-text">响应体</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%93%8D%E5%BA%94%E7%8A%B6%E6%80%81%E7%A0%81"><span class="toc-number">4.4.2.</span> <span class="toc-text">响应状态码</span></a></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Maven"><span class="toc-number">5.</span> <span class="toc-text">Maven</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#maven%E2%80%93%E9%A1%B9%E7%9B%AE%E6%9E%B6%E6%9E%84%E7%AE%A1%E7%90%86%E5%B7%A5%E5%85%B7"><span class="toc-number">5.1.</span> <span class="toc-text">maven–项目架构管理工具</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%8B%E8%BD%BD%EF%BC%8C%E5%AE%89%E8%A3%85%EF%BC%8C%E9%85%8D%E7%BD%AE%E7%8E%AF%E5%A2%83%E5%8F%98%E9%87%8F%EF%BC%8C%E9%85%8D%E7%BD%AE%E5%9B%BD%E5%86%85%E9%95%9C%E5%83%8F%EF%BC%8C%E9%85%8D%E7%BD%AE%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93%E2%80%93%E7%95%A5"><span class="toc-number">5.2.</span> <span class="toc-text">下载，安装，配置环境变量，配置国内镜像，配置本地仓库–略</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%9C%A8IDEA%E4%B8%AD%E4%BD%BF%E7%94%A8Maven"><span class="toc-number">5.3.</span> <span class="toc-text">在IDEA中使用Maven</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%88%9B%E5%BB%BA%E4%B8%80%E4%B8%AA%E6%99%AE%E9%80%9A%E7%9A%84maven%E9%A1%B9%E7%9B%AE"><span class="toc-number">5.4.</span> <span class="toc-text">创建一个普通的maven项目</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%A0%87%E8%AE%B0%E6%96%87%E4%BB%B6%E5%A4%B9%E7%9B%AE%E5%BD%95"><span class="toc-number">5.5.</span> <span class="toc-text">标记文件夹目录</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#pom%E6%96%87%E4%BB%B6"><span class="toc-number">5.6.</span> <span class="toc-text">pom文件</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#maven%E6%97%A0%E6%B3%95%E5%AF%BC%E5%87%BA%E6%88%96%E8%80%85%E7%94%9F%E6%95%88%E7%9A%84%E9%97%AE%E9%A2%98"><span class="toc-number">5.7.</span> <span class="toc-text">maven无法导出或者生效的问题</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#maven%E9%BB%98%E8%AE%A4web%E9%A1%B9%E7%9B%AE%E4%B8%AD%E7%9A%84web-xml%E7%89%88%E6%9C%AC%E9%97%AE%E9%A2%98"><span class="toc-number">5.8.</span> <span class="toc-text">maven默认web项目中的web.xml版本问题</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Serverlet"><span class="toc-number">6.</span> <span class="toc-text">Serverlet</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#servlet%E7%AE%80%E4%BB%8B"><span class="toc-number">6.1.</span> <span class="toc-text">servlet简介</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#HelloServlet"><span class="toc-number">6.2.</span> <span class="toc-text">HelloServlet</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Servlet%E5%8E%9F%E7%90%86"><span class="toc-number">6.3.</span> <span class="toc-text">Servlet原理</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Mapping%E9%97%AE%E9%A2%98"><span class="toc-number">6.4.</span> <span class="toc-text">Mapping问题</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#ServletContext"><span class="toc-number">6.5.</span> <span class="toc-text">ServletContext</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%AF%B7%E6%B1%82%E8%BD%AC%E5%8F%91"><span class="toc-number">6.6.</span> <span class="toc-text">请求转发</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%AF%BB%E5%8F%96%E8%B5%84%E6%BA%90%E6%96%87%E4%BB%B6"><span class="toc-number">6.7.</span> <span class="toc-text">读取资源文件</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#HttpServletResponse"><span class="toc-number">6.8.</span> <span class="toc-text">HttpServletResponse</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%AE%80%E5%8D%95%E5%88%86%E7%B1%BB"><span class="toc-number">6.8.1.</span> <span class="toc-text">简单分类</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%B4%9F%E8%B4%A3%E5%90%91%E6%B5%8F%E8%A7%88%E5%99%A8%E5%8F%91%E9%80%81%E6%95%B0%E6%8D%AE%E7%9A%84%E5%8A%9E%E6%B3%95"><span class="toc-number">6.8.1.1.</span> <span class="toc-text">负责向浏览器发送数据的办法</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%B4%9F%E8%B4%A3%E5%90%91%E6%B5%8F%E8%A7%88%E5%99%A8%E5%8F%91%E9%80%81%E5%93%8D%E5%BA%94%E5%A4%B4%E7%9A%84%E6%96%B9%E6%B3%95"><span class="toc-number">6.8.1.2.</span> <span class="toc-text">负责向浏览器发送响应头的方法</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%93%8D%E5%BA%94%E7%9A%84%E7%8A%B6%E6%80%81%E7%A0%81"><span class="toc-number">6.8.1.3.</span> <span class="toc-text">响应的状态码</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%B8%B8%E8%A7%81%E5%BA%94%E7%94%A8"><span class="toc-number">6.8.1.4.</span> <span class="toc-text">常见应用</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%AA%8C%E8%AF%81%E7%A0%81"><span class="toc-number">6.8.1.5.</span> <span class="toc-text">验证码</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%AE%9E%E7%8E%B0%E9%87%8D%E5%AE%9A%E5%90%91"><span class="toc-number">6.8.1.6.</span> <span class="toc-text">实现重定向</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#HttpServletRequest"><span class="toc-number">6.9.</span> <span class="toc-text">HttpServletRequest</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Cookie%E3%80%81Session"><span class="toc-number">7.</span> <span class="toc-text">Cookie、Session</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BC%9A%E8%AF%9D"><span class="toc-number">7.1.</span> <span class="toc-text">会话</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BF%9D%E5%AD%98%E4%BC%9A%E8%AF%9D%E7%9A%84%E4%B8%A4%E7%A7%8D%E6%8A%80%E6%9C%AF"><span class="toc-number">7.2.</span> <span class="toc-text">保存会话的两种技术</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Cookie"><span class="toc-number">7.3.</span> <span class="toc-text">Cookie</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Session-%E9%87%8D%E7%82%B9"><span class="toc-number">7.4.</span> <span class="toc-text">Session (重点)</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#javaBean"><span class="toc-number">8.</span> <span class="toc-text">javaBean</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#MVC%E4%B8%89%E5%B1%82%E6%9E%B6%E6%9E%84"><span class="toc-number">9.</span> <span class="toc-text">MVC三层架构</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Filter"><span class="toc-number">10.</span> <span class="toc-text">Filter</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Filter%E5%BC%80%E5%8F%91%E6%AD%A5%E9%AA%A4"><span class="toc-number">10.1.</span> <span class="toc-text">Filter开发步骤</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E7%9B%91%E5%90%AC%E5%99%A8"><span class="toc-number">11.</span> <span class="toc-text">监听器</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E8%BF%87%E6%BB%A4%E5%99%A8%E7%9B%91%E5%90%AC%E5%99%A8%E5%B8%B8%E8%A7%81%E5%BA%94%E7%94%A8"><span class="toc-number">12.</span> <span class="toc-text">过滤器监听器常见应用</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Junit-%E6%B5%8B%E8%AF%95"><span class="toc-number">13.</span> <span class="toc-text">Junit 测试</span></a></li></ol>
                
            </div>
        </div>
      </div>
    
  </div>

  <article>
    
        <div class="nexmoe-post-cover" style="padding-bottom: NaN%;"> 
            <img data-src="https://cdn.jsdelivr.net/gh/RhymeXmove/blogimg@latest/cover/JAVA.png" data-sizes="auto" alt="JAVA_javaweb" class="lazyload">
            <h1>JAVA_javaweb</h1>
        </div>
    
    
    <div class="nexmoe-post-meta nexmoe-rainbow-fill" style="margin:10px 0!important;">
    <a><i class="nexmoefont icon-calendar-fill"></i>2021年04月30日</a>
    <a><i class="nexmoefont icon-areachart"></i>7.7k 字</a>
    <a><i class="nexmoefont icon-time-circle-fill"></i>大概 37 分钟</a>
</div>

    <h1 id="基本概念"><a href="#基本概念" class="headerlink" title="基本概念"></a>基本概念</h1><span id="more"></span>

<h2 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h2><p>web开发：</p>
<ul>
<li>web，网页的意思，<a target="_blank" rel="noopener" href="http://www.baidu.com/">www.baidu.com</a></li>
<li>静态web<ul>
<li>html,css</li>
<li>提供给所有人看的数据始终不会发生变化！</li>
<li>技术栈：servlet / JSP，ASP，PHP</li>
</ul>
</li>
</ul>
<p>在java中，动态web资源开发的技术统称为JavaWeb</p>
<h2 id="web应用程序"><a href="#web应用程序" class="headerlink" title="web应用程序"></a>web应用程序</h2><p>web 应用程序：可以提供浏览器访问的程序；</p>
<ul>
<li>a.html、b.html ….. 多个web资源，这些web资源可以被外界访问，对外界提供服务；</li>
<li>你们能访问到的任何一个页面或者资源，都存在于这个世界上的某一角落的计算机上。</li>
<li>URL</li>
<li>这个统一的web资源会被放在同一个文件夹下，web应用程序–&gt;Tomcat: 服务器</li>
<li>一个web应用由多部分组成（静态web，动态web）<ul>
<li>html，css，js</li>
<li>jsp，servlet</li>
<li>java程序</li>
<li>jar包</li>
<li>配置文件（properties）</li>
</ul>
</li>
</ul>
<p>web应用程序编写完毕后，若想提供给外界访问：需要一个服务器来统一管理；</p>
<h2 id="静态web"><a href="#静态web" class="headerlink" title="静态web"></a>静态web</h2><ul>
<li><em>.html</em> 这些都是网页的后缀，如果服务器上一直存在这些东西，我们就可以直接进行读取。通络；</li>
</ul>
<p><img src="https://cdn.jsdelivr.net/gh/RhymeXmove/blogimg@latest/article/JAVA-javaweb-20210430/20210428131402.png"></p>
<ul>
<li>静态web存在的缺点<ul>
<li>web页面无法动态更新，所有用户看到都是同一个页面<ul>
<li>轮播图，点击特效；伪动态</li>
<li>javaScript （实际开发中，它用的最多）</li>
<li>VBScript</li>
</ul>
</li>
<li>它无法和数据库交互（数据无法持久化，用户无法交互）</li>
</ul>
</li>
</ul>
<h2 id="动态web"><a href="#动态web" class="headerlink" title="动态web"></a>动态web</h2><p>页面会动态展示：“web的页面展示的效果因人而异”；</p>
<p><img src="https://cdn.jsdelivr.net/gh/RhymeXmove/blogimg@latest/article/JAVA-javaweb-20210430/20210428131746.png"></p>
<p>缺点：</p>
<ul>
<li>加入服务器的动态web资源出现了错误，我们需要重新编写我们的后台程序，重新发布；<ul>
<li>停机维护</li>
</ul>
</li>
</ul>
<p>优点：</p>
<ul>
<li>Web页面可以动态刷新，所有用户看到的都不是同一个页面</li>
<li>他可以与数据库交互（数据持久化：注册，商品信息，用户信息……..）</li>
</ul>
<p><img src="https://cdn.jsdelivr.net/gh/RhymeXmove/blogimg@latest/article/JAVA-javaweb-20210430/20210428132115.png"></p>
<h1 id="Web服务器"><a href="#Web服务器" class="headerlink" title="Web服务器"></a>Web服务器</h1><p>ASP:</p>
<ul>
<li>微软：国内最早流行的就是ASP；</li>
<li>在HTML中嵌入了VB的脚本，ASP + COM；</li>
<li>在ASP开发中，基本一个页面都有几千行的代码业务，页面极其混乱</li>
<li>维护成本高！</li>
<li>C#</li>
<li>LLS</li>
</ul>
<pre><code class="asp">&lt;h1&gt;
    &lt;h1&gt;&lt;h1&gt;
        &lt;h1&gt;
            &lt;h1&gt;
                &lt;h1&gt;
        &lt;h1&gt;
            &lt;%
            System.out.println(&quot;hello&quot;)
                %&gt;
            &lt;h1&gt;
                &lt;h1&gt;
    &lt;h1&gt;&lt;h1&gt;
&lt;h1&gt;        
</code></pre>
<p>php :</p>
<ul>
<li>php 开发速度很快，功能很强大，跨平台，代码简单（70%，WordPress）</li>
<li>无法承载大访问量的情况（局限性）</li>
</ul>
<p>JSP/Servlet</p>
<p>B/S: 浏览和服务器</p>
<p>C/S: 客户端和服务器</p>
<ul>
<li>sun公司主推的B/S架构</li>
<li>基于java语言的（所有的大公司，或者一些开源的组件，都是用java写的）</li>
<li>可以承载三高问题带来的影响；</li>
<li>语法像ASP， ASP—&gt;JSP，加强市场强度；</li>
</ul>
<p>……</p>
<h2 id="web服务器"><a href="#web服务器" class="headerlink" title="web服务器"></a>web服务器</h2><p>服务器是一种被动的操作，用来处理用户的一些请求和给用户一些影响信息；</p>
<h3 id="IIS"><a href="#IIS" class="headerlink" title="IIS"></a>IIS</h3><p>微软的；ASP…, windows中自带的</p>
<h3 id="Tomcat"><a href="#Tomcat" class="headerlink" title="Tomcat"></a>Tomcat</h3><p>面向百度编程；</p>
<p>Tomcat 是 Apache 软件基金会（Apache Software Foundation） 的 Jakarta 项目中的一个核心项目，最新的Servlet 和 JSP 规范总是能在Tomcat 中的到体现，因为Tomcat技术先进，性能稳定，而且免费，因而深受java爱好者的喜爱并得到了部分软件开发商的认可，成为目前比较流行的web应用服务器。</p>
<p>Tomcat服务器是一个免费的开放源代码web应用服务器，属于轻量级应用服务器，在中小型系统和并发访问用户不是很多的场合下被普遍使用，是开发和调试jsp程序的首选。对于一个java初学web的人来说，它是最佳的选择。</p>
<p>Tomcat 实际上运行JSP页面和servlet。Tomcat 最新版本为9.0。</p>
<h1 id="Tomcat-1"><a href="#Tomcat-1" class="headerlink" title="Tomcat"></a>Tomcat</h1><h2 id="Tomcat启动和配置"><a href="#Tomcat启动和配置" class="headerlink" title="Tomcat启动和配置"></a>Tomcat启动和配置</h2><p>文件夹作用：</p>
<p><img src="https://cdn.jsdelivr.net/gh/RhymeXmove/blogimg@latest/article/JAVA-javaweb-20210430/20210428133849.png"></p>
<p><img src="https://cdn.jsdelivr.net/gh/RhymeXmove/blogimg@latest/article/JAVA-javaweb-20210430/20210428133859.png"></p>
<p>可以配置启动的端口号</p>
<ul>
<li>tomcat的默认端口号为：8080</li>
<li>mysql: 3306</li>
<li>http: 80</li>
<li>https: 443</li>
</ul>
<pre><code class="xml">&lt;Connector URIEncoding=&quot;UTF-8&quot; connectionTimeout=&quot;20000&quot; 
           port=&quot;8080&quot; protocol=&quot;HTTP/1.1&quot; redirectPort=&quot;8443&quot; useBodyEncodingForURI=&quot;true&quot;/&gt;
</code></pre>
<p>可以配置主机的名称</p>
<ul>
<li>默认的主机名为：localhost-&gt;127.0.0.1</li>
<li>默认网站应用存放的位置为：webapps</li>
</ul>
<pre><code class="xml">&lt;Host appBase=&quot;webapps&quot; autoDeploy=&quot;true&quot; name=&quot;localhost&quot; unpackWARs=&quot;true&quot;&gt;
</code></pre>
<p><strong>高难度面试题</strong>：</p>
<p>请你谈谈网站是如何进行访问的！</p>
<ol>
<li><p>输入一个域名: 回车</p>
</li>
<li><p>检查本机的 C:\Windows\System32\drivers\etc\hosts 配置文件下有没有这个域名映射：</p>
<ol>
<li><p>有：直接返回对应的IP地址，这个地址中，有我们需要访问的web程序，可以直接访问</p>
<pre><code class="powershell">192.30.255.112 github.com
</code></pre>
</li>
<li><p>没有：去DNS服务器找，找到的话就返回，找不到就返回找不到：</p>
</li>
</ol>
<p><img src="https://cdn.jsdelivr.net/gh/RhymeXmove/blogimg@latest/article/JAVA-javaweb-20210430/20210428135406.png"></p>
</li>
</ol>
<h2 id="发布一个网站"><a href="#发布一个网站" class="headerlink" title="发布一个网站"></a>发布一个网站</h2><p>将自己写的网站，放到服务器（Tomcat）中指定的Web应用的文件夹（Wevapps）</p>
<p>网站该有的结构</p>
<pre><code>--webapps：Tomcat服务器的web目录
    -ROOT
    -kuangstudy：网站的目录名
        -WEB-INF
            -classes：java程序
            -lib：web应用所依赖的jar包
            -web.xml：网站配置文件
        -index.html 默认的首页
        -static
            -css
                -style.css
            -js
            -img
        -......
</code></pre>
<h1 id="HTTP"><a href="#HTTP" class="headerlink" title="HTTP"></a>HTTP</h1><h2 id="什么是HTTP"><a href="#什么是HTTP" class="headerlink" title="什么是HTTP"></a>什么是HTTP</h2><p>HTTP（超文本传输协议）是一个简单的请求-响应协议，他通常运行在TCP之上。</p>
<ul>
<li>文本：html，字符串，~……</li>
<li>超文本：图片，音乐，视频，定位，地图……….</li>
<li>默认端口：80</li>
</ul>
<p>HTTPS：安全的</p>
<ul>
<li>默认端口：443</li>
</ul>
<h2 id="两个时代"><a href="#两个时代" class="headerlink" title="两个时代"></a>两个时代</h2><ul>
<li>http1.0<ul>
<li>HTTP/1.0：客户端可以与web服务器连接后，只能获得一个web资源，断开连接</li>
</ul>
</li>
<li>http2.0<ul>
<li>HTTP/1.1：客户端可以与web服务器连接后，可以获得多个web资源</li>
</ul>
</li>
</ul>
<h2 id="HTTP请求"><a href="#HTTP请求" class="headerlink" title="HTTP请求"></a>HTTP请求</h2><ul>
<li>客户端—发送请求（Request）— 服务器</li>
</ul>
<p>百度：</p>
<pre><code class="java">Request URL:https://www.baidu.com/ 请求地址
Request Method:GET     get方法/post方法
Status Code:200 OK     状态码：200
Remote （远程） Address：14.215.177.39:443
</code></pre>
<pre><code class="java">Accpet:text/html
Accept-Encoding:gzip, deflate, br
Accept-Language:zh-CN, zh; q=0.9 语言
Cache-Control:max-age=0
Connection:keep-alive
</code></pre>
<h3 id="请求行"><a href="#请求行" class="headerlink" title="请求行"></a>请求行</h3><ul>
<li><p>请求行中的请求方式：get</p>
</li>
<li><p>请求方式：Get，Post，HEAD，DELETE，PUT，TRACT……</p>
<ul>
<li>get: 请求能够携带的参数比较少，大小有限制，会在浏览器的URL地址栏显示数据内容，不安全，但高效；</li>
<li>post:请求能够携带的参数没有限制，大小没有限制，不会在浏览器的URL地址栏显示数据内容，安全，但不高效；</li>
</ul>
</li>
</ul>
<h3 id="消息头"><a href="#消息头" class="headerlink" title="消息头"></a>消息头</h3><pre><code class="java">Accpet: //告诉浏览器，它所支持的数据类型
Accept-Encoding: //支持哪种编码格式 GBK UTF-8 GB-2312 ISO8859-1
Accept-Language: //告诉浏览器，他的语言环境
Cache-Control:  //缓存控制
Connection: //告诉浏览器，请求完成是断开还是保持连接
HOST: 主机....../.
</code></pre>
<h2 id="HTTP响应"><a href="#HTTP响应" class="headerlink" title="HTTP响应"></a>HTTP响应</h2><ul>
<li>服务器—响应—客户端</li>
</ul>
<p>百度</p>
<pre><code class="xml">Cache-control:private  缓存控制
Connection：Keep-Alive  连接
Content-Encoding: gzip  编码
Content-Type:text/html  类型
</code></pre>
<h3 id="响应体"><a href="#响应体" class="headerlink" title="响应体"></a>响应体</h3><pre><code class="java">Accpet: //告诉浏览器，它所支持的数据类型
Accept-Encoding: //支持哪种编码格式 GBK UTF-8 GB-2312 ISO8859-1
Accept-Language: //告诉浏览器，他的语言环境
Cache-Control:  //缓存控制
Connection: //告诉浏览器，请求完成是断开还是保持连接
HOST: 主机....../.
Refresh: 告诉客户端，多久刷新一次；
Location: 让网页重新定位；
</code></pre>
<h3 id="响应状态码"><a href="#响应状态码" class="headerlink" title="响应状态码"></a>响应状态码</h3><p>200：请求响应成功</p>
<p>3xx：请求重定向</p>
<p>4xx：找不到资源</p>
<p>​    资源不存在；</p>
<p>5xx：服务器代码错误 500   502：网关错误</p>
<p>常见面试题：</p>
<p>当你的浏览器中地址栏输入地址并回车的一瞬间到页面能够展示回来，经历了什么?</p>
<ol>
<li>域名解析</li>
<li>发起TCP的三次握手</li>
<li>建立起TCP连接后发起http请求</li>
<li>服务器响应http请求，浏览器得到html代码</li>
<li>浏览器解析html代码，并请求html代码中的资源（css JavaScript 图片）</li>
<li>浏览器对页面进行渲染呈现</li>
<li>详细请参考:<a target="_blank" rel="noopener" href="https://www.cnblogs.com/wupeixuan/p/8747918.html">https://www.cnblogs.com/wupeixuan/p/8747918.html</a></li>
</ol>
<h1 id="Maven"><a href="#Maven" class="headerlink" title="Maven"></a>Maven</h1><p>为什么学习这个技术?</p>
<ol>
<li><p>在javaweb开发中, 需要使用大量的jar包,我们手动去导入;</p>
</li>
<li><p>如何能让一个东西自动帮我导入和配置这个jar包。</p>
<p>由此,maven诞生了。</p>
</li>
</ol>
<h2 id="maven–项目架构管理工具"><a href="#maven–项目架构管理工具" class="headerlink" title="maven–项目架构管理工具"></a>maven–项目架构管理工具</h2><p>我们目前用来就是方便导入jar包的！</p>
<p>Maven  的核心思想，<strong>约定大于配置</strong></p>
<ul>
<li>有约束，不要去违反。</li>
</ul>
<p>Maven会规定好你该如何去编写我们的java代码，必须要按照这个规范来。</p>
<h2 id="下载，安装，配置环境变量，配置国内镜像，配置本地仓库–略"><a href="#下载，安装，配置环境变量，配置国内镜像，配置本地仓库–略" class="headerlink" title="下载，安装，配置环境变量，配置国内镜像，配置本地仓库–略"></a>下载，安装，配置环境变量，配置国内镜像，配置本地仓库–略</h2><h2 id="在IDEA中使用Maven"><a href="#在IDEA中使用Maven" class="headerlink" title="在IDEA中使用Maven"></a>在IDEA中使用Maven</h2><p>创建maven项目</p>
<p><img src="https://cdn.jsdelivr.net/gh/RhymeXmove/blogimg@latest/article/JAVA-javaweb-20210430/20210428162308.png"></p>
<p><img src="https://cdn.jsdelivr.net/gh/RhymeXmove/blogimg@latest/article/JAVA-javaweb-20210430/20210428162631.png"></p>
<p><img src="https://cdn.jsdelivr.net/gh/RhymeXmove/blogimg@latest/article/JAVA-javaweb-20210430/20210428162806.png"></p>
<h2 id="创建一个普通的maven项目"><a href="#创建一个普通的maven项目" class="headerlink" title="创建一个普通的maven项目"></a>创建一个普通的maven项目</h2><p>不选模板创建</p>
<p>一个干净的maven项目</p>
<p><img src="https://cdn.jsdelivr.net/gh/RhymeXmove/blogimg@latest/article/JAVA-javaweb-20210430/20210428170220.png"></p>
<p>webapp文件夹只有在web应用下才会有！</p>
<h2 id="标记文件夹目录"><a href="#标记文件夹目录" class="headerlink" title="标记文件夹目录"></a>标记文件夹目录</h2><p><img src="https://cdn.jsdelivr.net/gh/RhymeXmove/blogimg@latest/article/JAVA-javaweb-20210430/20210428170647.png"></p>
<p>方法二</p>
<p><img src="https://cdn.jsdelivr.net/gh/RhymeXmove/blogimg@latest/article/JAVA-javaweb-20210430/20210428171043.png"></p>
<p><img src="https://cdn.jsdelivr.net/gh/RhymeXmove/blogimg@latest/article/JAVA-javaweb-20210430/20210428171331.png"></p>
<p><img src="https://cdn.jsdelivr.net/gh/RhymeXmove/blogimg@latest/article/JAVA-javaweb-20210430/20210428172011.png"></p>
<h2 id="pom文件"><a href="#pom文件" class="headerlink" title="pom文件"></a>pom文件</h2><pre><code class="xml">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;

&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
  xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
  &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;

  &lt;groupId&gt;com.kuang&lt;/groupId&gt;
  &lt;artifactId&gt;javaweb-01-maven&lt;/artifactId&gt;
  &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
  &lt;!-- package 项目的打包方式
   jar: java应用
   war: javaWeb应用
   --&gt;
  &lt;packaging&gt;war&lt;/packaging&gt;

  &lt;name&gt;javaweb-01-maven Maven Webapp&lt;/name&gt;
  &lt;!-- FIXME change it to the project&#39;s website --&gt;
  &lt;url&gt;http://www.example.com&lt;/url&gt;

  &lt;!-- 配置 --&gt;
  &lt;properties&gt;
    &lt;!-- 项目的默认构建编码 --&gt;
    &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
    &lt;!-- 编码版本 --&gt;
    &lt;maven.compiler.source&gt;1.7&lt;/maven.compiler.source&gt;
    &lt;maven.compiler.target&gt;1.7&lt;/maven.compiler.target&gt;
  &lt;/properties&gt;

  &lt;!-- 项目依赖 --&gt;
  &lt;dependencies&gt;
    &lt;!-- 具体依赖的jar包配置文件 --&gt;
    &lt;dependency&gt;
      &lt;groupId&gt;junit&lt;/groupId&gt;
      &lt;artifactId&gt;junit&lt;/artifactId&gt;
      &lt;version&gt;4.11&lt;/version&gt;
    &lt;/dependency&gt;
  &lt;/dependencies&gt;
  
  &lt;!-- 项目构建用的东西 --&gt;
  &lt;build&gt;
    &lt;finalName&gt;javaweb-01-maven&lt;/finalName&gt;
    &lt;pluginManagement&gt;&lt;!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --&gt;
      &lt;plugins&gt;
        &lt;plugin&gt;
          &lt;artifactId&gt;maven-clean-plugin&lt;/artifactId&gt;
          &lt;version&gt;3.1.0&lt;/version&gt;
        &lt;/plugin&gt;
        &lt;!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --&gt;
        &lt;plugin&gt;
          &lt;artifactId&gt;maven-resources-plugin&lt;/artifactId&gt;
          &lt;version&gt;3.0.2&lt;/version&gt;
        &lt;/plugin&gt;
        &lt;plugin&gt;
          &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
          &lt;version&gt;3.8.0&lt;/version&gt;
        &lt;/plugin&gt;
        &lt;plugin&gt;
          &lt;artifactId&gt;maven-surefire-plugin&lt;/artifactId&gt;
          &lt;version&gt;2.22.1&lt;/version&gt;
        &lt;/plugin&gt;
        &lt;plugin&gt;
          &lt;artifactId&gt;maven-war-plugin&lt;/artifactId&gt;
          &lt;version&gt;3.2.2&lt;/version&gt;
        &lt;/plugin&gt;
        &lt;plugin&gt;
          &lt;artifactId&gt;maven-install-plugin&lt;/artifactId&gt;
          &lt;version&gt;2.5.2&lt;/version&gt;
        &lt;/plugin&gt;
        &lt;plugin&gt;
          &lt;artifactId&gt;maven-deploy-plugin&lt;/artifactId&gt;
          &lt;version&gt;2.8.2&lt;/version&gt;
        &lt;/plugin&gt;
      &lt;/plugins&gt;
    &lt;/pluginManagement&gt;
  &lt;/build&gt;
&lt;/project&gt;
</code></pre>
<pre><code class="java">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot;
         xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
         xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;

    &lt;groupId&gt;com.kuang&lt;/groupId&gt;
    &lt;artifactId&gt;javaweb-01-maven02&lt;/artifactId&gt;
    &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;

    &lt;dependencies&gt;
        &lt;!-- maven的高级之处在于，他会帮你导入这个jar包所依赖的其他jar包 --&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;junit&lt;/groupId&gt;
            &lt;artifactId&gt;junit&lt;/artifactId&gt;
            &lt;version&gt;4.11&lt;/version&gt;
        &lt;/dependency&gt;
    &lt;/dependencies&gt;
&lt;/project&gt;
</code></pre>
<h2 id="maven无法导出或者生效的问题"><a href="#maven无法导出或者生效的问题" class="headerlink" title="maven无法导出或者生效的问题"></a>maven无法导出或者生效的问题</h2><p>maven由于他的约定大于配置，我们之后可能会遇到我们写的配置文件，无法被导出或者生效的问题，解决方案：</p>
<p>在build中配置resources节点，来防止我们资源导出失败的问题</p>
<pre><code class="xml">&lt;build&gt;
    .......
      &lt;resources&gt;
        &lt;resource&gt;
            &lt;directory&gt;src/main/resources&lt;/directory&gt;
            &lt;!--打包时该路径下过滤掉的--&gt;
            &lt;excludes&gt;  
                &lt;exclude&gt;**/*.properties&lt;/exclude&gt;
                &lt;exclude&gt;**/*.xml&lt;/exclude&gt;
             &lt;/excludes&gt;
            &lt;filtering&gt;true&lt;/filtering&gt;
        &lt;/resource&gt;
        &lt;resource&gt;
            &lt;directory&gt;src/main/java&lt;/directory&gt;
            &lt;!--打包时该路径下留下的，其他过滤掉--&gt;
            &lt;includes&gt;
                &lt;include&gt;**/*.properties&lt;/include&gt;
                &lt;include&gt;**/*.xml&lt;/include&gt;
            &lt;/includes&gt;
            &lt;filtering&gt;true&lt;/filtering&gt;
        &lt;/resource&gt;
    &lt;/resources&gt;
    ......
&lt;/build&gt;
</code></pre>
<h2 id="maven默认web项目中的web-xml版本问题"><a href="#maven默认web项目中的web-xml版本问题" class="headerlink" title="maven默认web项目中的web.xml版本问题"></a>maven默认web项目中的web.xml版本问题</h2><p>去tomcat\webapps\ROOT\WEB-INF\web.xml中复制，这里的是tomcat 9.0版本中的</p>
<pre><code class="xml">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;web-app xmlns=&quot;http://xmlns.jcp.org/xml/ns/javaee&quot;
  xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
  xsi:schemaLocation=&quot;http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd&quot;
  version=&quot;4.0&quot;
  metadata-complete=&quot;true&quot;&gt;

    
&lt;/web-app&gt;
</code></pre>
<h1 id="Serverlet"><a href="#Serverlet" class="headerlink" title="Serverlet"></a>Serverlet</h1><h2 id="servlet简介"><a href="#servlet简介" class="headerlink" title="servlet简介"></a>servlet简介</h2><ul>
<li>serverlet就是sun公司开发动态web的一门技术；</li>
<li>sun公司在这些API中提供一个接口叫做：Servlet，如果你想开发一个servlet程序，只需要完成两个小步骤：<ul>
<li>编写一个类，实现servlet接口。</li>
<li>把开发好的java类部署到web服务器中。</li>
</ul>
</li>
</ul>
<p><strong>把实现了Servlet接口的java程序叫做，servlet</strong></p>
<h2 id="HelloServlet"><a href="#HelloServlet" class="headerlink" title="HelloServlet"></a>HelloServlet</h2><p>Servlet接口在sun公司有两个默认的实现类：HttpServlet         GenericServlet</p>
<ol>
<li><p>构建一个普通maven项目，删掉其中的src目录，以后我们的学习就在这个项目里面建立Moudel；这个空的工程就是Maven主工程；</p>
</li>
<li><p>关于maven父子工程的理解：</p>
<ul>
<li>父项目中会有</li>
</ul>
<pre><code class="xml">&lt;modules&gt;
        &lt;module&gt;servlet-01&lt;/module&gt;
&lt;/modules&gt;
</code></pre>
<ul>
<li>子项目会有</li>
</ul>
<pre><code class="xml">&lt;parent&gt;
        &lt;artifactId&gt;javaweb-servlet&lt;/artifactId&gt;
        &lt;groupId&gt;com.kuang&lt;/groupId&gt;
        &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
&lt;/parent&gt;
</code></pre>
<p>父项目中的java子项目可以直接使用</p>
<pre><code class="java">son extends father
</code></pre>
</li>
<li><p>Maven环境优化</p>
<ol>
<li>修改<code>web.xml</code>为最新的</li>
<li>将maven的结构搭建完整</li>
</ol>
</li>
<li><p>编写一个Servlet程序</p>
<ol>
<li>编写一个普通类</li>
<li>实现Servlet接口</li>
<li>由于get或者post只是请求实现的不同方式，可以相互调用，业务逻辑都一样；</li>
</ol>
<pre><code class="java">import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class HelloServlet extends HttpServlet &#123;
    //由于get或者post只是请求实现的不同方式，可以相互调用，业务逻辑都一样；
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException &#123;
        PrintWriter writer = resp.getWriter(); //响应流
        writer.print(&quot;hello servlet&quot;);
    &#125;
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException &#123;
        super.doPost(req, resp);
    &#125;
&#125;
</code></pre>
</li>
<li><p>编写servlet的映射</p>
<p>为什么需要映射，我们写的是java程序，但是要通过浏览器访问，而浏览器需要连接Web服务器，所以我们需要在Web服务器中注册我们写的servlet，还需要给他一个浏览器能够访问到的路径；</p>
</li>
<li><p>配置tomcat</p>
</li>
<li><p>启动测试</p>
</li>
</ol>
<h2 id="Servlet原理"><a href="#Servlet原理" class="headerlink" title="Servlet原理"></a>Servlet原理</h2><p><img src="https://cdn.jsdelivr.net/gh/RhymeXmove/blogimg@latest/article/JAVA-javaweb-20210430/20210428201314.png"></p>
<h2 id="Mapping问题"><a href="#Mapping问题" class="headerlink" title="Mapping问题"></a>Mapping问题</h2><ol>
<li><p>一个servlet可以指定一个映射路径</p>
<pre><code class="xml">&lt;servlet-mapping&gt;
        &lt;servlet-name&gt;hello&lt;/servlet-name&gt;
        &lt;url-pattern&gt;/hello&lt;/url-pattern&gt;
&lt;/servlet-mapping&gt;
</code></pre>
</li>
<li><p>一个servlet可以指定对个映射路径</p>
<pre><code class="xml">&lt;!--注册servlet--&gt;
    &lt;servlet&gt;
        &lt;servlet-name&gt;hello&lt;/servlet-name&gt;
        &lt;servlet-class&gt;com.kuang.servlet.HelloServlet&lt;/servlet-class&gt;
    &lt;/servlet&gt;

    &lt;!--servlet的请求路径--&gt;
    &lt;servlet-mapping&gt;
        &lt;servlet-name&gt;hello&lt;/servlet-name&gt;
        &lt;url-pattern&gt;/hello&lt;/url-pattern&gt;
    &lt;/servlet-mapping&gt;
    &lt;servlet-mapping&gt;
        &lt;servlet-name&gt;hello&lt;/servlet-name&gt;
        &lt;url-pattern&gt;/hello1&lt;/url-pattern&gt;
    &lt;/servlet-mapping&gt;
    &lt;servlet-mapping&gt;
        &lt;servlet-name&gt;hello&lt;/servlet-name&gt;
        &lt;url-pattern&gt;/hello2&lt;/url-pattern&gt;
    &lt;/servlet-mapping&gt;
    &lt;servlet-mapping&gt;
        &lt;servlet-name&gt;hello&lt;/servlet-name&gt;
        &lt;url-pattern&gt;/hello3&lt;/url-pattern&gt;
    &lt;/servlet-mapping&gt;
</code></pre>
</li>
<li><p>一个servlet可以指定通用映射路径</p>
<pre><code class="xml">&lt;servlet-mapping&gt;
        &lt;servlet-name&gt;hello&lt;/servlet-name&gt;
        &lt;url-pattern&gt;/hello/*&lt;/url-pattern&gt;
&lt;/servlet-mapping&gt;
</code></pre>
</li>
<li><p>默认请求路径</p>
<pre><code class="xml">&lt;servlet-mapping&gt;
        &lt;servlet-name&gt;hello&lt;/servlet-name&gt;
        &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
&lt;/servlet-mapping&gt;
</code></pre>
</li>
<li><p>指定一些后缀或者前缀等等……</p>
<pre><code class="xml">&lt;servlet-mapping&gt;
        &lt;servlet-name&gt;hello&lt;/servlet-name&gt;
        &lt;url-pattern&gt;*.do&lt;/url-pattern&gt;
&lt;/servlet-mapping&gt;
可以自定义
&lt;servlet-mapping&gt;
        &lt;servlet-name&gt;hello&lt;/servlet-name&gt;
        &lt;url-pattern&gt;*.shan&lt;/url-pattern&gt;
&lt;/servlet-mapping&gt;
</code></pre>
</li>
<li><p>优先级问题</p>
<p>指定了固定的映射路径优先级最高，如果找不到就会走默认的处理请求</p>
<pre><code class="xml">    &lt;servlet&gt;
        &lt;servlet-name&gt;errorServlet&lt;/servlet-name&gt;
        &lt;servlet-class&gt;com.kuang.servlet.ErrorServlet&lt;/servlet-class&gt;
    &lt;/servlet&gt;

    &lt;servlet-mapping&gt;
        &lt;servlet-name&gt;errorServlet&lt;/servlet-name&gt;
        &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
    &lt;/servlet-mapping&gt;
</code></pre>
</li>
</ol>
<p>一般都是一对一</p>
<h2 id="ServletContext"><a href="#ServletContext" class="headerlink" title="ServletContext"></a>ServletContext</h2><p>web容器在启动的时候，它会为每个web程序都创建一个对应的ServletContext对象，他代表了当前的Web应用：</p>
<ul>
<li><p>共享容器</p>
<p>我在这个servlet中保存的数据，可以在另外一个servlet中拿到；（context.setAttribute();  context.getAttribute(); ）</p>
<p><img src="https://cdn.jsdelivr.net/gh/RhymeXmove/blogimg@latest/article/JAVA-javaweb-20210430/20210428230921.png"></p>
<pre><code class="java">@Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException &#123;
        ServletContext context = this.getServletContext();
        //this.getInitParameter();  //初始化参数
        //this.getServletConfig();  //servlet配置
        //this.getServletContext(); //servlet上下文
        resp.setContentType(&quot;text/html;charset=utf-8&quot;);
        String username = &quot;秦疆&quot;;
        context.setAttribute(&quot;username&quot;, username);
    &#125;
</code></pre>
<pre><code class="java">@Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException &#123;

        ServletContext context = this.getServletContext();
        String username = (String) context.getAttribute(&quot;username&quot;);
        resp.setContentType(&quot;text/html;charset=utf-8&quot;);
        resp.getWriter().print(&quot;名字&quot;+username);
    &#125;
</code></pre>
<pre><code class="xml">&lt;servlet&gt;
        &lt;servlet-name&gt;hello&lt;/servlet-name&gt;
        &lt;servlet-class&gt;com.kuang.servlet.HelloServlet&lt;/servlet-class&gt;
    &lt;/servlet&gt;
    &lt;servlet-mapping&gt;
        &lt;servlet-name&gt;hello&lt;/servlet-name&gt;
        &lt;url-pattern&gt;/hello&lt;/url-pattern&gt;
    &lt;/servlet-mapping&gt;

    &lt;servlet&gt;
        &lt;servlet-name&gt;getc&lt;/servlet-name&gt;
        &lt;servlet-class&gt;com.kuang.servlet.GetServlet&lt;/servlet-class&gt;
    &lt;/servlet&gt;
    &lt;servlet-mapping&gt;
        &lt;servlet-name&gt;getc&lt;/servlet-name&gt;
        &lt;url-pattern&gt;/getc&lt;/url-pattern&gt;
&lt;/servlet-mapping&gt;
</code></pre>
</li>
</ul>
<h2 id="请求转发"><a href="#请求转发" class="headerlink" title="请求转发"></a>请求转发</h2><pre><code class="java">protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException &#123;
        ServletContext context = this.getServletContext();
        System.out.println(&quot;servlet&quot;);
       // RequestDispatcher requestDispatcher = context.getRequestDispatcher(&quot;/servlet03&quot;); //请求转发的路径
      //  requestDispatcher.forward(req,resp); //调用foward实现请求转发
    
    context.getRequestDispatcher(&quot;/servlet03&quot;).forward(req,resp); //同上两行，调用foward实现请求转发
    &#125;
</code></pre>
<h2 id="读取资源文件"><a href="#读取资源文件" class="headerlink" title="读取资源文件"></a>读取资源文件</h2><p>Properties</p>
<ul>
<li>在java目录下新建properties</li>
<li>在resource目录下新建properties</li>
</ul>
<p>发现：都被打包到了同一个路径下：classes，我们俗称这个路径为classpath</p>
<pre><code class="java">@Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException &#123;
        resp.setContentType(&quot;text/html;charset=utf-8&quot;);
        InputStream is = this.getServletContext().getResourceAsStream(&quot;/WEB-INF/classes/db.properties&quot;);
        Properties prop = new Properties();
        prop.load(is);
        String username = prop.getProperty(&quot;username&quot;);
        String password = prop.getProperty(&quot;password&quot;);

        resp.getWriter().print(username+&quot;:&quot;+password);
    &#125;
</code></pre>
<p>访问测试都可OK；</p>
<h2 id="HttpServletResponse"><a href="#HttpServletResponse" class="headerlink" title="HttpServletResponse"></a>HttpServletResponse</h2><p>web服务器接收到客户端的HTTP请求，针对这个请求，分别创建一个代表请求的HttpServletRequest对象。代表相应的一个HttpServletResponse；</p>
<ul>
<li>我们如果要获取客户端请求过来的参数；找HttpServletRequest；</li>
<li>如果要给客户响应一些信息；找HttpServletResponse；</li>
</ul>
<h3 id="简单分类"><a href="#简单分类" class="headerlink" title="简单分类"></a>简单分类</h3><h4 id="负责向浏览器发送数据的办法"><a href="#负责向浏览器发送数据的办法" class="headerlink" title="负责向浏览器发送数据的办法"></a>负责向浏览器发送数据的办法</h4><pre><code class="java">ServletOutputStream getOutputStream() throw IOException;
PrintWriter getWriter() throws IOException;
</code></pre>
<h4 id="负责向浏览器发送响应头的方法"><a href="#负责向浏览器发送响应头的方法" class="headerlink" title="负责向浏览器发送响应头的方法"></a>负责向浏览器发送响应头的方法</h4><pre><code class="java">(ServletResponse)
void setCharacterEncoding(String charset);
void setContentLength(int len);
void setContentLengthLong(long len);
void setContentType(String type);
(HttpServletResponse)
void setDateHeader(String name, long date);
void addDateHeader(String name, long date);
void setHeader(String name, String value);
void addHeader(String name, String value);
void setIntHeader(String name, int value);
void addIntHeader(String name, int value);
</code></pre>
<h4 id="响应的状态码"><a href="#响应的状态码" class="headerlink" title="响应的状态码"></a>响应的状态码</h4><p>404…………………</p>
<h4 id="常见应用"><a href="#常见应用" class="headerlink" title="常见应用"></a>常见应用</h4><ol>
<li>向浏览器输出消息（前面内容）</li>
<li>下载文件<ol>
<li>要获取下载文件的路径</li>
<li>下载的文件名是什么</li>
<li>设置想办法让浏览器能够支持下载我们需要的东西</li>
<li>获取下载文件的输入流</li>
<li>创建缓冲区</li>
<li>获取OutputStream对象</li>
<li>将FileOutputStream流写入到buffer缓冲区</li>
<li>使用OutputStream将缓冲区中的数据输出到客户端！</li>
</ol>
</li>
</ol>
<pre><code class="java">@Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException &#123;
        //1. 要获取下载文件的路径
        String realPath = &quot;D:\\workspace\\IDEA-workspace\\kuangshen_java\\javaweb-servlet\\response\\src\\main\\resources\\1.png&quot;;
        System.out.println(&quot;下载文件的路径：&quot;+realPath);
        //2. 下载的文件名是什么
        String fileName = realPath.substring(realPath.lastIndexOf(&quot;\\&quot;) + 1);
        //3. 设置想办法让浏览器能够支持下载我们需要的东西
        resp.setHeader(&quot;Content-Disposition&quot;,&quot;attachment;filename=&quot;+URLEncoder.encode(fileName, &quot;utf-8&quot;));
        //4. 获取下载文件的输入流
        FileInputStream fileInputStream = new FileInputStream(realPath);
        //5. 创建缓冲区
        int len = 0;
        byte[] buffer = new byte[1024];
        //6. 获取OutputStream对象

        ServletOutputStream outputStream = resp.getOutputStream();
        //7. 将FileOutputStream流写入到buffer缓冲区
        //8. 使用OutputStream将缓冲区中的数据输出到客户端！
        while ((len=fileInputStream.read(buffer))&gt;0)&#123;
            outputStream.write(buffer,0,len);
        &#125;
        fileInputStream.close();
        outputStream.close();
    &#125;
</code></pre>
<h4 id="验证码"><a href="#验证码" class="headerlink" title="验证码"></a>验证码</h4><p>验证码怎么来的？</p>
<ul>
<li>前端实现</li>
<li>后端实现、需要用到java的图片类，生产一个图片</li>
</ul>
<pre><code class="java">public class ImageServlet extends HttpServlet &#123;
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException &#123;
        //如何让浏览器5秒自动刷新一次
        resp.setHeader(&quot;refresh&quot;,&quot;3&quot;);

        //在内存中创建一个图片
        BufferedImage image = new BufferedImage(80,20,BufferedImage.TYPE_INT_RGB);
        //的到图片
        Graphics2D g = (Graphics2D) image.getGraphics();
        //设置图片的背景颜色
        g.setColor(Color.white);
        g.fillRect(0,0,80,20);
        //给图片写数据
        g.setColor(Color.BLUE);
        g.setFont(new Font(null,Font.BOLD,20));
        g.drawString(makeNum(),0,20);
        //告诉浏览器这个请求用图片的方式打开
        resp.setContentType(&quot;image/jpeg&quot;);
        //网站存在缓存，不让浏览器缓存
        resp.setDateHeader(&quot;expires&quot;,-1);
        resp.setHeader(&quot;Cache-Control&quot;,&quot;no-cache&quot;);
        resp.setHeader(&quot;Pragma&quot;,&quot;no-cache&quot;);
        //把图片写给浏览器
        ImageIO.write(image,&quot;jpg&quot;, resp.getOutputStream());
    &#125;

    private String makeNum() &#123;
        Random random = new Random();
        String num = random.nextInt(9999999)+&quot;&quot;;
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i &lt; 7-num.length(); i++) &#123;
            sb.append(&quot;0&quot;);
        &#125;
        num = sb.toString() + num;
        return num;
    &#125;

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException &#123;
        super.doPost(req, resp);
    &#125;
&#125;
</code></pre>
<h4 id="实现重定向"><a href="#实现重定向" class="headerlink" title="实现重定向"></a>实现重定向</h4><p>B一个Web资源收到客户端A请求后，B会通知A客户端去访问另外一个Web资源C，这个过程叫做重定向；</p>
<p>常见场景：</p>
<ul>
<li>用户登录</li>
</ul>
<pre><code class="java">resp.sendRedirect(&quot;/index.jsp&quot;);
</code></pre>
<pre><code class="java">resp.setHeader(&quot;Location&quot;,&quot;/r/img&quot;);
        resp.setStatus(302);
</code></pre>
<p>面试题：</p>
<p>重定向和转发的区别？</p>
<p>相同点</p>
<ul>
<li>页面都会实现跳转</li>
</ul>
<p>不同点</p>
<ul>
<li>请求转发的时候，url不会产生变化  307</li>
<li>重定向的时候，URL地址会发生变化   302</li>
</ul>
<h2 id="HttpServletRequest"><a href="#HttpServletRequest" class="headerlink" title="HttpServletRequest"></a>HttpServletRequest</h2><pre><code class="java">@Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException &#123;
        req.setCharacterEncoding(&quot;utf-8&quot;);
        String username = req.getParameter(&quot;username&quot;);
        String password = req.getParameter(&quot;password&quot;);
        String[] hobbys = req.getParameterValues(&quot;hobby&quot;);

        System.out.println(username);
        System.out.println(password);
        System.out.println(Arrays.toString(hobbys));

        //通过请求转发
        req.getRequestDispatcher(&quot;./success.jsp&quot;).forward(req, resp);
    &#125;
</code></pre>
<h1 id="Cookie、Session"><a href="#Cookie、Session" class="headerlink" title="Cookie、Session"></a>Cookie、Session</h1><h2 id="会话"><a href="#会话" class="headerlink" title="会话"></a>会话</h2><p><strong>概念</strong>：用户打开一个浏览器，点击了很多超链接，访问了很多Web资源，关闭浏览器，这个过程称之为会话；</p>
<p><strong>有状态会话</strong>：</p>
<p>一个网站，怎么证明你来过？</p>
<p>客户端         服务端</p>
<ol>
<li>服务端给客户端一个信件，客户端下次访问服务端带上信件就可以了；cookies</li>
<li>服务端登记你来过了，下次你来的时候我来匹配你；session</li>
</ol>
<h2 id="保存会话的两种技术"><a href="#保存会话的两种技术" class="headerlink" title="保存会话的两种技术"></a>保存会话的两种技术</h2><p>cookie</p>
<ul>
<li>客户端技术（响应，请求）</li>
</ul>
<p>session</p>
<ul>
<li>服务端技术，利用这个技术，可以保存用户的会话信息？我们可以把信息或者数据放在session中！</li>
</ul>
<p>常见场景：网站登陆之后，下次不用登陆了，第二次访问直接就登上了！</p>
<h2 id="Cookie"><a href="#Cookie" class="headerlink" title="Cookie"></a>Cookie</h2><ol>
<li>从请求中拿到cookie信息。</li>
<li>服务器响应给客户端cookie。</li>
</ol>
<p>各种常用方法：</p>
<pre><code class="java">@Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException &#123;
        //服务器，告诉你，你来的时间，把这个时间封装成一个信件，你下次带来，我就知道你来了
        //解决中文乱码
        resp.setContentType(&quot;text/html&quot;);
        req.setCharacterEncoding(&quot;utf-8&quot;);
        resp.setCharacterEncoding(&quot;utf-8&quot;);

        PrintWriter out = resp.getWriter();

        //Cookie，服务器端从客户端获取时
        Cookie[] cookies = req.getCookies();  //这里返回数组，说明cookies可能存在多个
        //判断Cookies是否存在
        if (cookies!=null) &#123;
            //如果cookies存在
            out.write(&quot;你上一次访问的时间是：&quot;);

            for (int i = 0; i &lt; cookies.length; i++) &#123;
                Cookie cookie = cookies[i];
                //获取cookie的值 cookie.getValue();
                if (cookie.getName().equals(&quot;LastLoginTime&quot;)) &#123;
                    //获取cookie的名字
                    long l = Long.parseLong(cookie.getValue());
                    Date date = new Date(l);
                    out.print(date.toLocaleString());
                &#125;

            &#125;
        &#125;else &#123;
            out.write(&quot;这是你第一次访问本站&quot;);
        &#125;
        // 服务器给客户端响应一个cookie
        Cookie cookie = new Cookie(&quot;LastLoginTime&quot;, System.currentTimeMillis()+&quot;&quot;);

        //设置cookie有效期，关闭浏览器不会删除cookie
        cookie.setMaxAge(24*60*60);
        //响应给客户端一个cookie，可以多个
        /*
        resp.addCookie(cookie);
        resp.addCookie(cookie);
        resp.addCookie(cookie);
        */
        resp.addCookie(cookie);
    &#125;
</code></pre>
<p>cookie：一般会保存在本地的用户目录下appdata:</p>
<p>一个网站的cookie是否存在上限！<strong>细节问题</strong></p>
<ul>
<li>一个cookie只能保存一个信息；</li>
<li>一个web站点/域名可以给浏览器发送多个cookie，最多存放20个；</li>
<li>所有cookie总大小不超出4kb;</li>
<li>浏览器总共能储存300个cookie；</li>
<li>具体情况根据浏览器不同会有不同限制；</li>
</ul>
<p>删除cookie：</p>
<ul>
<li>不设置有效期，关闭浏览器，自动失效；</li>
<li>设置有效期时间为0；</li>
</ul>
<p>PS：解码的一个方法</p>
<pre><code class="java">URLDecoder.decode(String s, &quot;UTF-8&quot;);
</code></pre>
<h2 id="Session-重点"><a href="#Session-重点" class="headerlink" title="Session (重点)"></a>Session (重点)</h2><p>什么是Session：</p>
<ul>
<li>服务器会给每一个用户（浏览器）创建一个session对象；</li>
<li>一个session独占一个浏览器，只要浏览器没有关闭，这个session就存在；</li>
<li>用户登录之后，整个网站他都可以访问！–&gt;保存用户信息，保存购物车的信息……</li>
</ul>
<p>Session和cookie的区别：</p>
<ul>
<li>Cookie是把用户的数据写给用户的浏览器，浏览器保存（可以保存）；</li>
<li>Session把用户的数据写到用户独占session中，服务器端保存；（减少服务器资源的浪费）</li>
<li>Session对象由服务创建；</li>
</ul>
<p>使用场景：</p>
<ul>
<li>保存一个登录用户的信息；</li>
<li>购物车信息；</li>
<li>在整个网站中经常会使用的数据，我们将它保存在session中；</li>
</ul>
<p>使用Session:</p>
<pre><code class="java">    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException &#123;
        //解决中文乱码
        resp.setContentType(&quot;text/html；charset=utf-8&quot;);
        req.setCharacterEncoding(&quot;utf-8&quot;);
        resp.setCharacterEncoding(&quot;utf-8&quot;);

        //得到session
        HttpSession session = req.getSession();
        //给session中存东西
        session.setAttribute(&quot;name&quot;, new Person(&quot;秦疆&quot;, 1));
        //获取sessionID
        String id = session.getId();
        //判断session是否是新创建的
        if (session.isNew()) &#123;
            resp.getWriter().write(&quot;session创建成功，ID&quot;+id);
        &#125; else &#123;
            resp.getWriter().write(&quot;session已经在服务器中存在，ID&quot;+id);
        &#125;
    &#125;
</code></pre>
<pre><code class="java">//得到session
HttpSession session = req.getSession();
//给session中存东西
Person person = (Person) session.getAttribute(&quot;name&quot;);
resp.getWriter().write(person.toString());
</code></pre>
<pre><code class="java">//移除session
HttpSession session = req.getSession();
session.removeAttribute(&quot;name&quot;);
</code></pre>
<pre><code class="java">  &lt;!--设置session失效时间--&gt;
  &lt;session-config&gt;
    &lt;!--15分钟后失效，timeout以分钟为单位--&gt;
    &lt;session-timeout&gt;15&lt;/session-timeout&gt;
  &lt;/session-config&gt;
</code></pre>
<h1 id="javaBean"><a href="#javaBean" class="headerlink" title="javaBean"></a>javaBean</h1><p>实体类</p>
<p>javaBean有特定的写法：</p>
<ul>
<li>必须要有一个无参构造；</li>
<li>属性必须私有化；</li>
<li>必须有对应的get/set方法；</li>
</ul>
<p>一般用来和数据库的字段做映射  ORM；</p>
<p>ORM对象关系映射</p>
<ul>
<li>表-&gt;类</li>
<li>字段-&gt;属性</li>
<li>行记录-&gt;对象</li>
</ul>
<p><strong>people表</strong></p>
<table>
<thead>
<tr>
<th>id</th>
<th>name</th>
<th>age</th>
<th>address</th>
</tr>
</thead>
<tbody><tr>
<td>1</td>
<td>秦疆1号</td>
<td>3</td>
<td>西安</td>
</tr>
<tr>
<td>2</td>
<td>秦疆2号</td>
<td>18</td>
<td>西安</td>
</tr>
<tr>
<td>3</td>
<td>秦疆3号</td>
<td>100</td>
<td>西安</td>
</tr>
</tbody></table>
<h1 id="MVC三层架构"><a href="#MVC三层架构" class="headerlink" title="MVC三层架构"></a>MVC三层架构</h1><p>什么是MVC：model，view，controller  模型，视图，控制器</p>
<p><img src="https://cdn.jsdelivr.net/gh/RhymeXmove/blogimg@latest/article/JAVA-javaweb-20210430/20210430181656.png"></p>
<p>用户直接访问控制层，控制层就可以直接操作数据库：</p>
<pre><code class="java">servlet --CURD ---&gt;数据库
弊端：程序十分臃肿，不利于维护
servlet的代码中；处理请求、响应、视图跳转、处理JDBC，处理业务代码，处理逻辑代码    
    
架构：没有什么是加了一层解决不了的......
</code></pre>
<p>Model</p>
<ul>
<li>业务处理：业务逻辑（service）</li>
<li>数据持久层：CURD （Dao）</li>
</ul>
<p>View</p>
<ul>
<li>展示数据</li>
<li>提供链接发起servlet请求（a，form，img…….）</li>
</ul>
<p>Controller (Servlet)</p>
<ul>
<li>接收用户的请求：（req：请求参数、Session信息……）</li>
<li>交给业务层处理对应的代码</li>
<li>控制视图的跳转</li>
</ul>
<pre><code class="java">登录 ---接受用户的请求 --- 处理用户的请求（获取用户登录的参数，username，password） --- 交给业务层处理登录业务（判断用户名密码是否正确：事务）--- Dao层查询用户名和密码是否正确 --- 数据库
</code></pre>
<h1 id="Filter"><a href="#Filter" class="headerlink" title="Filter"></a>Filter</h1><p>FIlter：过滤器，用来过滤网站数据；</p>
<ul>
<li>处理中文乱码</li>
<li>登录验证</li>
</ul>
<p><img src="https://cdn.jsdelivr.net/gh/RhymeXmove/blogimg@latest/article/JAVA-javaweb-20210430/20210430200702.png"></p>
<h2 id="Filter开发步骤"><a href="#Filter开发步骤" class="headerlink" title="Filter开发步骤"></a>Filter开发步骤</h2><ol>
<li><p>导包</p>
</li>
<li><p>编写过滤器</p>
<pre><code class="java">package com.kuang.filter;

import javax.servlet.*;
import java.io.IOException;

public class CharacterEncodingFilter implements Filter &#123;
    //初始化
    public void init(FilterConfig filterConfig) throws ServletException &#123;
        System.out.println(&quot;CharacterEncodingFilter初始化&quot;);
    &#125;

    //  chain（链）
    /*
    过滤中的所有代码，在过滤特定请求的时候都会执行
    必须要让过滤器继续通行  chain.doFilter(request,response);
     */
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException &#123;
        request.setCharacterEncoding(&quot;utf-8&quot;);
        response.setCharacterEncoding(&quot;utf-8&quot;);
        response.setContentType(&quot;text/html;charset=utf-8&quot;);
        System.out.println(&quot;CharacterEncodingFilter执行前......&quot;);
        chain.doFilter(request,response);//让请求继续走，如果不写，程序会被这里拦截
        System.out.println(&quot;CharacterEncodingFilter执行后......&quot;);
    &#125;

    //销毁
    public void destroy() &#123;
        System.out.println(&quot;CharacterEncodingFilter销毁&quot;);
    &#125;
&#125;
</code></pre>
</li>
<li><p>在web.xml中配置Filter</p>
<pre><code class="java">    &lt;filter&gt;
        &lt;filter-name&gt;characterEncodingFilter&lt;/filter-name&gt;
        &lt;filter-class&gt;com.kuang.filter.CharacterEncodingFilter&lt;/filter-class&gt;
    &lt;/filter&gt;

    &lt;filter-mapping&gt;
        &lt;filter-name&gt;characterEncodingFilter&lt;/filter-name&gt;
        &lt;!--此访问路径（不是项目路径）下的任何请求，都会经过这个过滤器--&gt;
        &lt;url-pattern&gt;/servlet/*&lt;/url-pattern&gt;
    &lt;/filter-mapping&gt;
</code></pre>
</li>
<li><p><strong>Filter链</strong></p>
<p>​    在一个web应用中，可以开发编写多个Filter，这些Filter组合起来称为一个Filter链。</p>
<p>​    web服务器根据Filter在web.xml中的注册顺序，决定先调用哪个Filter，当第一个Filter的doFilter方法被调用时，web服务器会创建一个代表Filter链的FilterChain对象传递给该方法，在doFilter方法中，开发人员如果调用了FilterChain对象的doFilter方法，则web服务器会检查FilterChain对象中是否还有filter，如果有，则调用第二个filter，如果没有，则调用目标资源。</p>
</li>
</ol>
<h1 id="监听器"><a href="#监听器" class="headerlink" title="监听器"></a>监听器</h1><p>监听登录人数</p>
<pre><code class="java">package com.kuang.listener;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

public class OnlineCountListener implements HttpSessionListener &#123;
    //创建session监听，看你的一举一动
    //一旦创建session就会触发一次这个事践
    public void sessionCreated(HttpSessionEvent se) &#123;
        ServletContext ctx = se.getSession().getServletContext();
        Integer onlineCount = (Integer) ctx.getAttribute(&quot;OnlineCount&quot;);
        if (onlineCount==null) &#123;
            onlineCount = 1;
        &#125;else &#123;
            int count = onlineCount;
            onlineCount = count + 1;
        &#125;
        ctx.setAttribute(&quot;onlineCount&quot;, onlineCount);
    &#125;
    //销毁session监听
    public void sessionDestroyed(HttpSessionEvent se) &#123;

    &#125;
&#125;
</code></pre>
<pre><code class="java">&lt;listener&gt;
    &lt;listener-class&gt;com.kuang.listener.OnlineCountListener&lt;/listener-class&gt;
&lt;/listener&gt;
&lt;session-config&gt;
    &lt;session-timeout&gt;1&lt;/session-timeout&gt;
&lt;/session-config&gt;
</code></pre>
<h1 id="过滤器监听器常见应用"><a href="#过滤器监听器常见应用" class="headerlink" title="过滤器监听器常见应用"></a>过滤器监听器常见应用</h1><p>用户登录后才能进入主页！用户注销后就不能进入主页了！</p>
<ol>
<li>用户登录之后，向session中放入用户的数据；</li>
<li>进入主页的时候要判断用户是否已经登录；下面：在过滤器中实现；</li>
</ol>
<pre><code class="java">package com.kuang.filter;

import com.kuang.utils.Constant;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class SysFilter implements Filter &#123;
    public void init(FilterConfig filterConfig) throws ServletException &#123;
    &#125;

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException &#123;

        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;

        if (request.getSession().getAttribute(Constant.USER_SESSION)==null)&#123;
            response.sendRedirect(&quot;/sys/Error.jsp&quot;);
        &#125;

        chain.doFilter(req,resp);
    &#125;
    public void destroy() &#123;
    &#125;
&#125;
</code></pre>
<h1 id="Junit-测试"><a href="#Junit-测试" class="headerlink" title="Junit 测试"></a>Junit 测试</h1><p><code>@Test</code>注解</p>
<p>只能在单元测试代码中使用</p>

  </article>

  
    
  <div class="nexmoe-post-copyright">
    <strong>本文作者：</strong>shanX<br>
    <strong>本文链接：</strong><a href="https://rhymexmove.github.io/2021/04/30/6cdc310e6784/" title="https:&#x2F;&#x2F;rhymexmove.github.io&#x2F;2021&#x2F;04&#x2F;30&#x2F;6cdc310e6784&#x2F;" target="_blank" rel="noopener">https:&#x2F;&#x2F;rhymexmove.github.io&#x2F;2021&#x2F;04&#x2F;30&#x2F;6cdc310e6784&#x2F;</a><br>
    
      <strong>版权声明：</strong>本文采用 <a href="https://creativecommons.org/licenses/by-nc-sa/3.0/cn/deed.zh" target="_blank">CC BY-NC-SA 3.0 CN</a> 协议进行许可
    
  </div>


  
  
  <div class="nexmoe-post-meta nexmoe-rainbow">
    
        <a class="nexmoefont icon-appstore-fill -link" href="/categories/java/">java</a>
    
    
        <a class="nexmoefont icon-tag-fill -none-link" href="/tags/java%E5%9F%BA%E7%A1%80/" rel="tag">java基础</a> <a class="nexmoefont icon-tag-fill -none-link" href="/tags/%E7%8B%82%E7%A5%9E%E8%AF%B4/" rel="tag">狂神说</a>
    
</div>

  <div class="nexmoe-post-footer">
    <section class="nexmoe-comment">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/gitalk@1.7.2/dist/gitalk.min.css">
<div id="gitalk"></div>
<script src="https://cdn.jsdelivr.net/npm/gitalk@1.7.2/dist/gitalk.min.js"></script>
<script type="text/javascript">
    var gitalk = new Gitalk({
        clientID: 'a2d666d91d6af126f538',
        clientSecret: '9c79e44494cd9fae312629457ae9b534e01a963e',
        id: decodeURI(window.location.pathname),
        repo: 'RhymeXmove.github.io',
        owner: 'RhymeXmove',
        admin: 'RhymeXmove'
    })
    gitalk.render('gitalk')
</script>
</section>
  </div>
</div>
                            <div class="nexmoe-post-right">
                                <div class="nexmoe-fixed">
                                    <div class="nexmoe-tool">
                                        <a href="#nexmoe-content" class="toc-link" aria-label="回到顶部" title="top"><button class="mdui-fab mdui-ripple"><i class="nexmoefont icon-caret-top"></i></button></a>
                                    </div>
                                </div>
                            </div>
                    </div>
                </div>
                <script src="https://cdn.jsdelivr.net/combine/npm/lazysizes@5.1.0/lazysizes.min.js,gh/highlightjs/cdn-release@9.15.8/build/highlight.min.js,npm/mdui@0.4.3/dist/js/mdui.min.js?v=1"></script>
<script>
	hljs.initHighlightingOnLoad();
</script>

<script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/gh/fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.js"></script>


<script src="https://cdn.jsdelivr.net/gh/xtaodada/xtaodada.github.io@0.0.2/copy.js"></script>
 

<script src="/js/app.js?v=1649858550922"></script>

<script src="https://cdn.jsdelivr.net/npm/justifiedGallery@3.8.1/dist/js/jquery.justifiedGallery.min.js"></script>
<script>
	$(".justified-gallery").justifiedGallery({
		rowHeight: 160,
		margins: 10,
	});
</script>

                    





        </body>

</html>